# Maintainer: Tim S <stahta01@gmail.com>
# ArchLinux Contributor: Pierre-Marie de Rodat <pmderodat on #ada at freenode.net>
# ArchLinux Contributor: Rod Kay <charlie5 on #ada at freenode.net>


_realname=gprbuild-bootstrap
pkgbase=mingw-w64-${_realname}-git
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}-git
_gh_release=21.0.0
pkgver=20${_gh_release}
pkgrel=1

pkgdesc="Static GPRbuild to bootstrap XML/Ada and GPRbuild itself (mingw-w64)"
arch=('any')
url='https://github.com/AdaCore/gprbuild/'
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-gcc-ada")
provides=("${MINGW_PACKAGE_PREFIX}-gprbuild-bootstrap")
conflicts=("${MINGW_PACKAGE_PREFIX}-gprbuild"
           "${MINGW_PACKAGE_PREFIX}-gprbuild-gpl"
           "${MINGW_PACKAGE_PREFIX}-gprbuild-bootstrap")
source=("gprbuild-${_gh_release}.tar.gz::https://github.com/AdaCore/gprbuild/archive/v${_gh_release}.tar.gz"
        "gprconfig_kb-${_gh_release}.tar.gz::https://github.com/AdaCore/gprconfig_kb/archive/v${_gh_release}.tar.gz"
        "xmlada-${_gh_release}.tar.gz::https://github.com/AdaCore/xmlada/archive/v${_gh_release}.tar.gz")
sha256sums=('54b7d1a3298160109aaee4d8c263c1ab3ab4abae75d354f3e90a4c51639167a2'
            '2aec26afad5bb1a4685d9c041c9c797ff5beda211a5e81f2a97452d2ceabc557'
            '923024931f0c57451aa52cb9a3333874646102cb75957f27e3689670f90edc1e')

prepare() {
  cd ${srcdir}/gprbuild-${_gh_release}/

  # GPRbuild hard-codes references to ${MINGW_PREFIX}/libexec, but MINGW packages
  # must use ${MINGW_PREFIX}/lib instead.
  sed -i "s|libexec|lib|g" doinstall gprbuild.gpr \
    ${srcdir}/gprconfig_kb-${_gh_release}/db/compilers.xml \
    ${srcdir}/gprconfig_kb-${_gh_release}/db/linker.xml \
    ${srcdir}/gprconfig_kb-${_gh_release}/db/gnat.xml

  build_date=$(date '+%Y%m%d')
  release_year=$(echo ${pkgver} | cut -d. -f1)

  # Set version information
  sed -i "s/18.0w/${pkgver}-bootstrap/" gpr/src/gpr-version.ads
  sed -i "s/19940713/${build_date}/" gpr/src/gpr-version.ads
  sed -i "s/2016/${release_year}/" gpr/src/gpr-version.ads
  sed -i "s/:= Gnatpro;/:= FSF;/" gpr/src/gpr-version.ads

  mv ${srcdir}/gprconfig_kb-${_gh_release} ${srcdir}/gprconfig_kb/

}

build() {
  cd ${srcdir}/gprbuild-${_gh_release}/

  export GNATMAKEFLAGS="-j$(nproc)"
  export DESTDIR="${srcdir}/bootstrap"
  ./bootstrap.sh \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=/lib \
    --with-xmlada="${srcdir}/xmlada-${_gh_release}/"
}

package() {
  cd "$srcdir/bootstrap"
  cp -a --no-preserve=ownership -- "${srcdir}/bootstrap/${MINGW_PREFIX}" "${pkgdir}"
}
